package com.hanson.controller;

import com.hanson.common.bean.ResultMessage;
import com.hanson.entity.UserAddresBean;
import com.hanson.service.UserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/web/address")
public class WebAddressController {
    @Autowired
    private UserAddressService userAddressService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(@RequestBody UserAddresBean userAddresBean, @CookieValue("openId") String openId) throws Exception {
        userAddressService.addAddress(openId, userAddresBean);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("新增地址成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultMessage modify(@RequestBody UserAddresBean userAddresBean, @CookieValue("openId") String openId) throws Exception {
        userAddressService.updateAddress(openId, userAddresBean);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("修改地址成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public ResultMessage delete(@PathVariable String id, @CookieValue("openId") String openId) throws Exception {
        userAddressService.deleteAddress(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("删除地址成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/updateIsDefault/{id}/{isDefault}", method = RequestMethod.POST)
    public ResultMessage updateIsDefault(@CookieValue("openId") String openId,@PathVariable String id, @PathVariable String isDefault) throws Exception {
        userAddressService.updateIsDefault(openId,isDefault, id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("修改成功！");
        return resultMessage;
    }

}
